//
//  CommentViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "CommentViewController.h"
#import "FirstPageModel.h"
#import "CommentCell.h"
#import "CommentModel.h"

@interface CommentViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *commentModelArray;//model数组

@property (nonatomic, retain) UITableView *tableView;

@end

@implementation CommentViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //视图将要出现的时候刷新
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor wheatColor];
    self.commentModelArray = [NSMutableArray array];
    
    //导航栏标题
    self.navigationItem.title = @"评论";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //导航栏左侧按钮
    UIImage *image = [UIImage imageNamed:@"iconfont-unie642.png"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)  style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    
    //取消cell线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self getAFNetWorkingData:self.firstPageModel.id];//请求cell数据
}

- (void)getAFNetWorkingData:(NSInteger)ids
{
    NSString *urlString = [NSString stringWithFormat:@"http://api.bohejiaju.com/v1/posts/%ld/comments?limit=20&offset=0", ids];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dictionary = responseObject;
        NSMutableArray *array = [[dictionary objectForKey:@"data"] objectForKey:@"comments"];
        
        for (NSMutableDictionary *dic in array)
        {
            CommentModel *commentModel = [[CommentModel alloc] init];
            [commentModel setValuesForKeysWithDictionary:dic];
            
            [self.commentModelArray addObject:commentModel];
            
        }

        [self.tableView reloadData];
        
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             NSLog(@"error = %@",error);
             //本地缓存
         }];
}

//指定分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//指定多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.commentModelArray.count;
    
}

//创建一个cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //创建自定义cell
    static NSString *cellIndentifier = @"cell";
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil)
    {
        cell = [[CommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    //恢复cell线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //设置cell点击效果 (没有置灰效果)
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CommentModel *commentModel = [self.commentModelArray objectAtIndex:indexPath.row];
    cell.commentModel = commentModel;
    return cell;
}

//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return HEIGHT / 6;
    
}



- (void)leftBarAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
